import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
import Home from '../views/Home.vue'//直接引入页面
import TableManagement from '@/views/Table/TableManagement.vue'
import ChartManagement from '../views/Table/ChartManagement.vue'
import FormManagement from '../views/Table/FormManagement.vue'
import Welcome from '@/views/Welcome.vue'



Vue.use(VueRouter)

const routes: Array<RouteConfig> = [
  {
    path:'/',
    redirect:'/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login/Login.vue')//按需加载页面(路由懒加载)
  },
  {
    path: '/password',
    name: 'password',
    component: () => import('@/views/Login/Password.vue')
  },
  {
    path: '/home',
    name: 'home',
    redirect:'/Welcome',
    component: Home,
    children:[
      { 
        path: '/welcome', 
        name:'欢迎管理员',
        component: Welcome },
      {
        path:'/TableManagement',
        name:'表格管理',
        component:TableManagement
      },
      {
        path:'/ChartManagement',
        name:'图表管理',
        component:ChartManagement
      },
      {
        path:'/FormManagement',
        name:'表单管理',
        component:FormManagement
      },
      {
        path:'/home/AccountManagement',
        name:'账户管理',
        component:()=>import('@/views/Account/AccountManagement.vue')//按需加载
      }
    ]
  },
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//路由导航
router.beforeEach((to: any, from: any, next: any) => {
  const isLogin = localStorage.tsToken ? true : false
  if (to.path == '/login' || to.path == '/password') {
    next()
  } else {
    isLogin ? next() : next('/login')
  }
})

export default router
